package edu.study.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.DepartmentApi;
import edu.study.user.api.dto.DepartmentAndSubDto;
import edu.study.user.api.dto.DepartmentDto;
import edu.study.user.api.req.department.AddDepartmentReq;
import edu.study.user.api.req.department.DeleteDepartmentReq;
import edu.study.user.api.req.department.ModifyDepartmentReq;
import edu.study.user.api.req.department.ModifyDepartmentStatusReq;
import edu.study.user.api.req.department.QueryDepartmentPageListReq;
import edu.study.user.api.req.department.QueryDepartmentReq;
import edu.study.user.service.DepartmentService;

@RestController
public class DepartmentApiImpl implements DepartmentApi {

	@Autowired
    private Lock lock;

    @Autowired
    private DepartmentService departmentService;

	@Override
	public BaseRespDto addDepartment(@RequestBody AddDepartmentReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentName(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentService.addDepartment(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteDepartment(@RequestBody DeleteDepartmentReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentService.deleteDepartment(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyDepartment(@RequestBody ModifyDepartmentReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentService.modifyDepartment(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyDepartmentStatus(@RequestBody ModifyDepartmentStatusReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getDepartmentNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = departmentService.modifyDepartmentStatus(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<DepartmentDto> queryDepartment(@RequestBody QueryDepartmentReq req) {
		RespDto<DepartmentDto> resp = departmentService.queryDepartment(req);
        return resp;
	}

	@Override
	public PageRespDto<DepartmentAndSubDto> queryDepartmentPageList(@RequestBody QueryDepartmentPageListReq req) {
		PageRespDto<DepartmentAndSubDto> resp = departmentService.queryDepartmentPageList(req);
        return resp;
	}
	
	@Override
	public RespDto<List<DepartmentDto>> queryDepartmentAndSubList(@RequestBody QueryDepartmentReq req) {
		RespDto<List<DepartmentDto>> resp = departmentService.queryDepartmentAndSubList(req);
        return resp;
	}
	
	@Override
	public RespDto<DepartmentAndSubDto> queryDepartmentAndSub(@RequestBody QueryDepartmentReq req) {
		RespDto<DepartmentAndSubDto> resp = departmentService.queryDepartmentAndSub(req);
        return resp;
	}
}
